<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$wetterSource = file_get_contents('http://data.wetter.info/wetterapp/wetterAppController.php?mapName=xxlradar-de&layers=radar&enableStandalone=iframe&enableHighlighting=false&enableCityClick=false&enableSelector=false&autoplay=true&enableShowTimestamp=true');

preg_match_all("([0-9]{12}\.gif)", $wetterSource, $treffer, PREG_SET_ORDER);
exec ('rm -f /tmp/rain/*.gif');
exec ('rm -f /tmp/*.gif');
$i=0;
$files = "";
// print_r($treffer);
foreach ($treffer as $wert) {
	// echo "hallo " . $wert[0];
	$image = file_get_contents("http://data.wetter.info//data/layers/xxlradar-de/xxlradar-de_radar_" . $wert[0]);
	$files = $files . " /tmp/rain/" . $wert[0];
	$name = "/tmp/" .  $wert[0];
	file_put_contents($name, $image);
	try
	{
	$picture = new Imagick($name);
	$img2 = new Imagick( "images/hintergrund.gif" );
	$img3 = new Imagick( "images/muenster.gif" );


	$picture->cropImage(700, 500, 0, 0);
	$picture->setImagePage(0, 0, 0, 0); // Remove canvas

	$UTCdatum = substr($wert[0],0,12);

	$dateSrc = substr($UTCdatum,0,4)."-". substr($UTCdatum,4,2) . "-" . substr($UTCdatum,6,2)." ".substr($UTCdatum,8,2).":".substr($UTCdatum,10,2)." GMT";
	$dateTime = new DateTime($dateSrc);




	$tekst = date('H:i:s', strtotime($dateSrc));
	$draw = new ImagickDraw();

	/* Black text */
	$draw->setFillColor('black');

	/* Font properties */
	$draw->setFont('images/arial.ttf');
	$draw->setFontSize( 18 );

	/* Create text */
	$picture->annotateImage($draw, 16, 31, 0,$tekst);
	


	$draw->setFillColor('white');
	$picture->annotateImage($draw, 15, 30, 0,$tekst);
		/* Merge image with Background */
	//$img2->setImageColorspace($picture->getImageColorspace() ); 

	//$img2->compositeImage( $picture, imagick::COMPOSITE_DEFAULT, 0, 0 );
		$picture->compositeImage( $img3, imagick::COMPOSITE_DEFAULT, 237, 372 );

	 $picture->writeImage('/tmp/rain/' . $wert[0]);

}
catch(Exception $e)
{
        echo $e->getMessage();
}

	$i++;
}
$result = exec("/usr/bin/convert -delay 80 -loop 0 -dispose previous " . $files . " /var/www/wetter/images/rain.gif 2>&1");
$result = exec("/etc/image.sh");
header('Content-Type: image/gif');
readfile("/var/www/wetter/images/rain2.gif");
?>
